package com.shundr.shipper.truck.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.shipper.frame.constant.LCommands;
import com.shundr.shipper.frame.http.HttpResult;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.http.LHttpRequest.RequestCompleteListener;
import com.shundr.shipper.truck.model.TruckInfo;
import com.shundr.shipper.truck.model.UserTrackInfo;
/**
 * 车辆Request
 * @author Administrator
 *
 */
public class UserTruckRequest extends LHttpRequest implements
		RequestCompleteListener {
	public static final int GET_TRUCK_LIST_SUCCESSED = 1;// 获取车辆列表成功
	public static final int GET_TRUCK_INFO_SUCCESSED = 2;// 获取车辆详情成功
	public static final int ADD_TRUCK_SUCCESSED=3;//增加车辆成功
	public static final int DELETE_TRUCK_SUCCESSED=4;//删除车辆成功
	public static final int UPDATE_TRUCK_SUCCESSED=5;//修改车辆信息成功
	
	public static final int GET_FAM_TRUCK_LIST_SUCCESSED = 6;// 获取我的熟车车列表成功
	public static final int DELETE_FAM_TRUCK_SUCCESSED=7;//删除熟车成功
	public static final int ADD_FAM_TRUCK_SUCCESSED=8;//添加熟车成功
	public static final int GET_SEARCH_FAM_TRUCK_LIST_SUCCESSED = 9;// 查询我的熟车车列表成功
	
	public static final int UPDATE_TRUCK_STATUS_SUCCESSED = 10;//更新车辆状态成功
	public static final int PUBLISH_EMPTY_TRUCK_SUCCESSED = 11;//发布空车成功
	
	public static final int GET_USER_TRACK_LIST_SUCCESSED = 12;// 获取个人轨迹列表
	
	public static final int GET_SEARCH_PERSONAL_TRUCK_LIST_SUCCESS=25;//获取个人车源列表成功

	private Handler mHandler;
	private Message mMessage;
	private Context mContext;

	private Map<String, Object> param;
	private String command;

	public UserTruckRequest(Context context, Handler handler) {
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}

	/**
	 * 
	 * getTruckList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param truckStatus
	 * @param validationStatus
	 * @param offset
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getTruckList(int truckStatus,int validationStatus, int offset, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("truckStatus", truckStatus);
		param.put("truckValidationStatus", validationStatus);
		param.put("offset", offset);
		param.put("pageSize", pageSize);
		command = LCommands.GET_TRUCK_LIST;
		requestByPost(param);
	}
	
	/**
	 * 获取在途管理数据
	 * getTruckMap(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param startTime
	 * @param expiryTime
	 * @param startIndex
	 * @param userId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getTruckMap(int userId,int startIndex,String startTime,String expiryTime) {
		param = new HashMap<String, Object>();
		param.put("userId", userId);
		param.put("startTime", startTime);
		param.put("expiryTime", expiryTime);
		param.put("startIndex", startIndex);
		command = LCommands.GET_TRUCK_MAP;
		requestByGet(param);
	}
	
    /**
     * getTruckInfo
     * @param truckId
     */
	public void getTruckInfo(int truckId) {
		param = new HashMap<String, Object>();
		param.put("truckId", truckId);
		command = LCommands.GET_TRUCK_INFO;
		requestByPost(param);
	}

	/**
	 * 
	 * addTruck(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param truckidNum
	 * @param truckTypeCode
	 * @param truckLoad
	 * @param truckLength
	 * @param expectAddrTxtAll
	 * @param expectAddrCodeAll
	 * @param truckstatus
	 * @param lng
	 * @param lat
	 * @param address
	 * @param addressCode
	 * @param base64
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void addTruck(String truckidNum, String truckTypeCode,
			String truckLoad, String truckLength, String expectAddrTxtAll,
			String expectAddrCodeAll, int truckstatus, double lng, double lat,
			String address, String addressCode, String base64) {
		param = new HashMap<String, Object>();
		param.put("truckidNum", truckidNum);
		param.put("truckTypeCode", truckTypeCode);
		param.put("truckLoad", truckLoad);
		param.put("truckLength", truckLength);
		param.put("expectAddrTxtAll", expectAddrTxtAll);
		param.put("expectAddrCodeAll", expectAddrCodeAll);
		param.put("truckstatus", truckstatus);
		param.put("fileObjectListJson", base64);

		param.put("locationLng", lng);
		param.put("locationLat", lat);
		param.put("currentAddrTxt", address);
		param.put("currentAddrCode", addressCode);
		command = LCommands.ADD_TRUCK;
		requestByPost(param);
	}

	/**
	 * deleteTruck
	 * @param truckId
	 */
	public void deleteTruck(int truckId) {
		param = new HashMap<String, Object>();
		param.put("truckId", truckId);
		command = LCommands.DELETE_MYTRUCK_BYID;
		requestByPost(param);
	}

	/**
	 * 更新车辆信息
	 * updateTruck(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param truckidNum
	 * @param truckTypeCode
	 * @param truckLoad
	 * @param truckLength
	 * @param expectAddrTxtAll
	 * @param expectAddrCodeAll
	 * @param truckstatus
	 * @param lng
	 * @param lat
	 * @param address
	 * @param addressCode
	 * @param base64
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void updateTruck(String truckid, String truckTypeCode,
			String truckLoad, String truckLength, String expectAddrTxtAll,
			String expectAddrCodeAll, double lng, double lat,
			String address, String addressCode, String base64) {
		param = new HashMap<String, Object>();
		param.put("truckid", truckid);
		param.put("truckTypeCode", truckTypeCode);
		param.put("truckLoad", truckLoad);
		param.put("truckLength", truckLength);
		param.put("expectAddrTxtAll", expectAddrTxtAll);
		param.put("expectAddrCodeAll", expectAddrCodeAll);
		param.put("fileObjectListJson", base64);

		param.put("locationLng", lng);
		param.put("locationLat", lat);
		param.put("currentAddrTxt", address);
		param.put("currentAddrCode", addressCode);
		command = LCommands.UPDATE_TRUCK;
		requestByPost(param);
	}
	
	/**
	 * 查询车源列表
	 * @param srcPlaceCode	出发地
	 * @param dstPlaceCode	目的地
	 * @param type			车辆类型
	 * @param length		车辆长度
	 * @param weight		车辆载重
	 * @param startIndex	
	 * @param size			
	 */
	public void getSearchPersonalTruckList(String srcPlaceCode, String dstPlaceCode, String type, 
			String length, String weight, int startIndex, int size) {
		
		if (startIndex == 0) {
//			getSearchPersonalTruckNum(srcPlaceCode, dstPlaceCode, type, length, weight);
		}
		
		param = new HashMap<String, Object>();
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		param.put("truckTypeCode", type);
		param.put("truckLength", length);
		param.put("truckCapacity", weight);
		param.put("startSize", startIndex);
		param.put("size", size);
		command = LCommands.SEARCH_PERSONAL_TRUCK_LIST;
		requestByGet(param);
	}
	
	public void getSearchPersonalTruckNum(String srcPlaceCode, String dstPlaceCode, String type, 
			String length, String weight) {
		param = new HashMap<String, Object>();
		param.put("srcPlaceCode", srcPlaceCode);
		param.put("dstPlaceCode", dstPlaceCode);
		param.put("truckTypeCode", type);
		param.put("truckLength", length);
		param.put("truckCapacity", weight);
		command = LCommands.SEARCH_PERSONAL_TRUCK_LIST_NUM;
		requestByGet(param);
	}
	
	/**
	 * getFamTruckList
	 * @param offset
	 * @param pageSize
	 */
	public void getFamTruckList(int offset, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("offset", offset);
		param.put("pageSize", pageSize);
		command = LCommands.GET_FAM_TRUCK_LIST;
		requestByPost(param);
	}
	
	/**
	 * 
	 * getSearchFamTruckList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param mobile
	 * @param plate
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getSearchFamTruckList(String mobile, String plate) {
		param = new HashMap<String, Object>();
		param.put("phoneNum", mobile);
		param.put("truckPlanteNum", plate);
		command = LCommands.GET_SEARCH_FAM_TRUCK_LIST;
		requestByPost(param);
	}
	
	/**
	 * deleteTruck
	 * @param truckId
	 */
	public void deleteFamTruck(int truckId) {
		param = new HashMap<String, Object>();
		param.put("truckId", truckId);
		command = LCommands.DELETE_FAM_TRUCK_BY_ID;
		requestByPost(param);
	}
	
	/**
	 * 添加熟车
	 * addFamTruck(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param truckId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void addFamTruck(int truckId){
		param = new HashMap<String, Object>();
		param.put("truckId", truckId);
		command = LCommands.ADD_FAM_TRUCK;
		requestByPost(param);
	}
	
	/**
	 * 更改车辆状态为接单中
	 * updateTruckStatus(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param truckId
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void updateTruckStatus(int truckId,int status) {
		param = new HashMap<String, Object>();
		param.put("truckid", truckId);
		param.put("truckStatus", status);
		command = LCommands.UPDATE_TRUCK_STATUS;
		requestByPost(param);
	}
	
	/**
	 * 发布空车
	 * publishEmptyTruck(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param truckid
	 * @param currentCode
	 * @param expectAddrTxtAll
	 * @param expectAddrCodeAll
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void publishEmptyTruck(String truckid, String currentCode,
			String expectAddrTxtAll, String expectAddrCodeAll) {
		param = new HashMap<String, Object>();
		param.put("truckid", truckid);
		param.put("currentCityCode", currentCode);
		param.put("expectAddrTxtAll", expectAddrTxtAll);
		param.put("expectAddrCodeAll", expectAddrCodeAll);
		command = LCommands.PUBLISH_EMPTY_TRUCK;
		requestByPost(param);
	}

	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if(mHandler == null)
			return;
		if (command.equals(LCommands.GET_TRUCK_LIST)) {
			mMessage.what = GET_TRUCK_LIST_SUCCESSED;
			List<TruckInfo> infos = UserTruckParser.parserTruckList(result.getResult());
			mMessage.obj = infos;				
		}else if (command.equals(LCommands.ADD_TRUCK)) {
			mMessage.what = ADD_TRUCK_SUCCESSED;	    				
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.DELETE_MYTRUCK_BYID)) {
			mMessage.what = DELETE_TRUCK_SUCCESSED;							
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.UPDATE_TRUCK)) {
			mMessage.what = UPDATE_TRUCK_SUCCESSED;			
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.GET_TRUCK_INFO)) {
			mMessage.what = GET_TRUCK_INFO_SUCCESSED;
			TruckInfo infos = UserTruckParser.parserTruckInfo(result.getResult());
			mMessage.obj = infos;	
		} else if (command.equals(LCommands.SEARCH_PERSONAL_TRUCK_LIST)) {
			mMessage.what = GET_SEARCH_PERSONAL_TRUCK_LIST_SUCCESS;
			List<TruckInfo> infos = UserTruckParser.parserTruckList(result.getResult());
			mMessage.obj = infos;
		}else if (command.equals(LCommands.GET_FAM_TRUCK_LIST)) {
			mMessage.what = GET_FAM_TRUCK_LIST_SUCCESSED;
			List<TruckInfo> infos = UserTruckParser.parserTruckList(result.getResult());
			mMessage.obj = infos;				
		}else if (command.equals(LCommands.DELETE_FAM_TRUCK_BY_ID)) {
			mMessage.what = DELETE_FAM_TRUCK_SUCCESSED;							
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.GET_SEARCH_FAM_TRUCK_LIST)) {
			mMessage.what = GET_SEARCH_FAM_TRUCK_LIST_SUCCESSED;
			List<TruckInfo> infos = UserTruckParser.parserTruckList(result.getResult());
			mMessage.obj = infos;				
		}else if (command.equals(LCommands.ADD_FAM_TRUCK)) {
			mMessage.what = ADD_FAM_TRUCK_SUCCESSED;							
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.UPDATE_TRUCK_STATUS)) {
			mMessage.what = UPDATE_TRUCK_STATUS_SUCCESSED;							
			mMessage.obj = result.getDescription();
		}else if (command.equals(LCommands.PUBLISH_EMPTY_TRUCK)) {
			mMessage.what = PUBLISH_EMPTY_TRUCK_SUCCESSED;							
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.GET_TRUCK_MAP)){
			mMessage.what = GET_USER_TRACK_LIST_SUCCESSED;
			List<UserTrackInfo> infos = UserTruckParser.parserTruckMap(result.getResult());
			mMessage.obj = infos;	
		} else if(command.equals(LCommands.SEARCH_PERSONAL_TRUCK_LIST_NUM) 
				|| command.equals(LCommands.GET_CARGO_LIST_NUM)) {
			return;
		}
		mHandler.sendMessage(mMessage);
	}

	@Override
	public void requestFailed(HttpResult result) {
		if(mHandler == null)
			return;
		// 请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
